<template>
  <el-card shadow="never">
    <template #header>
      <div style="display: flex;">
        <lbtn  @click="setDay(1)" :type="current===1?'primary':'default'">今天</lbtn>
        <lbtn  @click="setDay(2)" :type="current===2?'primary':'default'">昨天</lbtn>
        <lbtn  @click="setDay(3)" :type="current===3?'primary':'default'">前天</lbtn>
        <div  style="margin-left: 10px; width: 220px;margin-right: 30px;">
          <JPicker style="width: 100%" v-model="billDate" size="default" type="daterange" />
        </div>
        <div style="margin-right: 10px;">
          <ltext v-model="billNo" placeholder="票号" />
        </div>
        <tiny-select clearable size="small" v-model="deliverId" placeholder="送货员" :searchable="true" :show-empty-image="true" style="width: 200px;">
          <tiny-option v-for="item in delivers" :key="item.id" :label="item.name" :value="item.id"> </tiny-option>
        </tiny-select>
        <lbtn  @click="search" style="margin-left: 10px;" icon="search">搜索</lbtn >
        <lbtn  @click="download" style="margin-left: 10px;" icon="download">下载</lbtn >
        <lbtn  @click="clear" style="margin-left: 10px;" icon="refresh">刷新</lbtn >
      </div>
    </template>
    <!--      <el-empty description="请扫描配送单二维码" />-->
    <el-container>
      <el-aside width="720px"  >
          <t-grid :data-list="psList" size="large" :show-footer="false" :show-page="false"  :fields="cols1" :offsetHeight="10" :remote-sort="false" />
      </el-aside>
      <el-main style="padding: 0;">
        <div style="margin-left: 10px;">
            <t-grid ref="gref" size="large" :show-footer="false" :show-page="false"  :fields="cols2" :offsetHeight="10" :data-list="goods" :remote-sort="false" />
        </div>
      </el-main>
    </el-container>
  </el-card>

</template>
<script>
// 出货统计
import tool from "@/utils/tool";
import { Select, Option } from '@opentiny/vue'

export default {
  name: 'pstjm',
  props: {
    showClose: {type: Boolean, default: false}
  },
  components: {
    TinySelect: Select,
    TinyOption: Option
  },
  data () {
    return {
      offsetH: 310,
      list: [],
      delivers:[],
      cols1: [
        {type: 'index', width: 40},
        {field: 'psNo', title: '配送号', width: 100,},
        {field: 'billDate', title: '配送时间', width: 90,},
        {field: 'bills', title: '票号', width: 110,align: 'left'},
        {field: 'buName', title: '客户', width: 200,align: 'left'},
        {field: 'deliverNames', title: '送货人', width: 90,align: 'left'},
        {field: 'state', title: '状态',align: 'center',width: 70, },
      ],
      cols2: [
        {type: 'index', width: 40},
        {field: 'invName', title: '商品名称',width: 200,align:'left',
          filter: true,sortable: true,
        },
        {field: 'spec', title: '规格', width: 60,align: 'center',},
        {field: 'unit', title: '单位', width: 60,align: 'center',},
        {field: 'qty', title: '数量',align: 'right' ,width: 80,sortable: true,},
        {field: 'assQty', title: '辅助数量',align: 'right' ,width: 130,sortable: true,},
      ],
      current: 1,
      billDate: [tool.getDay(),tool.getDay()],
      deliverId: '',
      billNo: '',

    }
  },
  created() {
    tool.post('common/dropdown', {cat: 'adminList'}).then(ret => {
      if(ret.stat) {
        this.delivers =ret.data
      }
    })
    this.search()
  },
  computed: {
    gh () {
      return window.innerHeight - this.offsetH
    },
    psList () {
      let ret = []
      let gp = tool.xe.groupBy(this.list, 'billNo')
      for(let i in gp) {
        ret.push({
          psNo: gp[i][0].psNo,
          bills: gp[i][0].billNo,
          billDate: gp[i][0].billDate,
          buName: gp[i][0].buName,
          state: gp[i][0].state,
          deliverNames: gp[i][0].deliverNames,
        })
      }
      return ret
    },
    goods () {
      let ret = []
      let gp = tool.xe.groupBy(this.list, 'invId')
      for(let i in gp) {
        let good = {
          invId: gp[i][0].invId,
          invName: gp[i][0].invName,
          spec: gp[i][0].spec,
          unit: gp[i][0].unit2?gp[i][0].unit2:gp[i][0].unit1,
          unit1: gp[i][0].unit1,
          unit2: gp[i][0].unit2,
          qty: tool.xe.sum(gp[i], 'qty'),
          assQty: ''
        }
        good.assQty = tool.toAssQty(good)
        ret.push(good)
      }
      return tool.xe.orderBy(ret, [['qty', 'desc']])
    }
  },
  methods: {
    close () {
      this.$router.go(-1)
    },
    scanPs ({code}) {
      let arr = code.split('-')
      // let billId = arr[1]
      if (Number(arr[0]) !== 180001) {
        return tool.err('非配送票据')
      }
      if (Number(arr[0]) === 180001) {
        tool.post('scan/findPsBill', {scanInfo: code}).then(ret => {
          if (ret.stat && ret.data) {
            for (let i = 0; i < this.list.length; i++) {
              if(this.list[i].id === ret.data.id) {
                return
              }
            }
            this.list.push(ret.data)
          }
        })
      }
    },
    clear () {
      this.deliverId = ''
      this.list = []
      this.search()
    },
    setDay (index) {
      this.current = index
      if(index === 1) {
        this.billDate =  [tool.getDay(),tool.getDay()]
      }
      if(index === 2) {
        this.billDate =  [tool.getDay('', -1),tool.getDay('', -1)]
      }
      if(index === 3) {
        this.billDate =  [tool.getDay('', -2),tool.getDay('', -2)]
      }
      this.search()
    },
    download () {
       this.search(1)
    },
    search (excel = 0) {
      this.list = []
      tool.post('deliver/gpCargos', {
        billDate: this.billDate,
        deliverId: this.deliverId?this.deliverId:0,
        billNo: this.billNo,
        excel
      }, true).then(ret => {
        if (ret.stat && ret.data) {
          if(excel === 1) {
           window.open(ret.data)
          }
          else {
            this.list = ret.data
          }
        }
      })
    }
  }
}
</script>